﻿using System;
using Accona.Remoting;
using Accona.Test.Common;

namespace Accona.Test.Server
{
    class Server
    {
        static void Main(string[] args)
        {
            // This call will parse the assembly for classes marked with the RemoteExecutionHandlerAttribute. These classes will
            // be used to serve incoming request for a remote method call, i.e. the class will handle the incoming calls.
            RemoteExecutionRepository.Instance.ParseAssembly(typeof(Server).Assembly);
            
            // The RemotingRepository will start the necessary communication for remoting
            var rep = new RemotingRepository();

            // An alternative way to register interface handler instead of an attribute
            //rep.Setup().Resolve<IMyInterface>().With(ResolveRequest);

            // The repoisotory will resolve the interface to the local implementation, i.e. the ParseAssembly above
            // will find the class MyInterfaceImplementation and return an instance of that class.
            //var intrfc = rep.Resolve<IMyInterface>();

            // The variable intrfc is now a direct instance of MyInterfaceImplementation.  
            //Console.Write("Executed locally : ");          
            //intrfc.DoStuff(); // This is just to verify that this will not generate a remote call.

            //Now, we let the server run until application exits by waiting for a key to be pressed. The communication is open
            // and the server will serve incoming request until application terminates.
            Console.ReadKey();
        }

        private static IMyInterface ResolveRequest()
        {
            return new MyInterfaceImplementation();
        }
    }
}
